package org.glandais.games.rebondrobots;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.glandais.games.rebondrobots.board.Board;
import org.glandais.games.rebondrobots.board.Goal;
import org.glandais.games.rebondrobots.board.Robots;
import org.glandais.games.rebondrobots.solver.BruteForceSolver;
import org.glandais.games.rebondrobots.solver.RobotMove;
import org.glandais.games.rebondrobots.solver.Solution;

/**
 * Hello world!
 * 
 */
public class App {
	public static void main(String[] args) throws IOException {
		File imgFolder = new File("img");
		FileUtils.deleteDirectory(imgFolder);
		imgFolder.mkdirs();
		// fullRandom();
		sameBoard();
	}

	private static void sameBoard() {
		Random r = new Random();

		Board board = Board.createRandomBoard(r);
		List<Goal> goals = board.getGoals();
		Robots robots = Robots.createRandomRobots(board, r);

		for (int game = 0; game < 50; game++) {
			Collections.shuffle(goals);
			boolean done = false;
			for (Goal goal : goals) {
				if (!done) {
					List<RobotMove> moves = getSolution(board, robots, goal);
					int movesSize = moves.size();
					if (movesSize > 6) {
						done = true;
						robots = executeMoves(board, game, goal, moves);
					}
				}
			}

			if (!done) {
				Goal goal = goals.get(0);
				List<RobotMove> moves = getSolution(board, robots, goal);
				robots = executeMoves(board, game, goal, moves);
			}

		}

	}

	private static Robots executeMoves(Board board, int game, Goal goal,
			List<RobotMove> moves) {
		Robots robots;
		int i = 0;
		int movesSize = moves.size();
		for (RobotMove robotMove : moves) {
			board.saveToPNG(
					new File("img/" + game + ".solution."
							+ format(i, movesSize) + ".png"),
					robotMove.getRobots(), robotMove, goal);
			i++;
		}
		robots = moves.get(moves.size() - 1).getRobots();
		return robots;
	}

	private static List<RobotMove> getSolution(Board board, Robots robots,
			Goal goal) {
		BruteForceSolver solver = new BruteForceSolver(board, robots, goal);
		Solution solution = solver.solve();
		List<RobotMove> moves = solution.getMoves();
		return moves;
	}

	private static void fullRandom() {
		for (int seed = 193; seed < 200; seed++) {
			Random r = new Random(seed);

			Board board = Board.createRandomBoard(r);
			List<Goal> goals = board.getGoals();
			Goal goal = goals.get(r.nextInt(goals.size()));
			Robots robots = Robots.createRandomRobots(board, r);

			// board.saveToPNG(new File("img/" + seed + "board.png"), robots,
			// null, goal);

			List<RobotMove> moves = getSolution(board, robots, goal);
			int i = 0;
			int movesSize = moves.size();
			if (movesSize > 5) {
				for (RobotMove robotMove : moves) {
					board.saveToPNG(new File("img/" + seed + ".solution."
							+ format(i, movesSize) + ".png"),
							robotMove.getRobots(), robotMove, goal);
					i++;
				}
			}
			System.out.println(seed);
		}
	}

	private static String format(int i, int movesSize) {
		if (movesSize > 10) {
			return StringUtils.leftPad(Integer.toString(i), 2, "0");
		}
		return Integer.toString(i);
	}
}
